#include "drscontrol.h"
#include <QTcpSocket>
#include <QTimer>
#include <QMessageBox>
#include <QScrollBar>
#include <QStyleOptionTitleBar>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QTextDocumentWriter>
#include <QIcon>
#include "qextserialport.h"
#include "version.h"
#include "firewire.h"
#include "usbasyncmgr.h"
#include "deviceSetup.h"


DRSControl::DRSControl(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	Graph *graph;
	ui.setupUi(this);
	mDeviceSetupDialog = new DeviceSetup(this);
	mDeviceSetupDialog->exec();
	Usb * usb = Usb::getInstance();
	connect(ui.tcpPushButton,SIGNAL(clicked()),this,SLOT(on_inputLineEdit_returnPressed()));
	windowTitle.sprintf("Radio Comm Program - Version %s", VERSION);
	setWindowTitle(windowTitle);
	setWindowIcon(QIcon("computer.ico"));
	gVhpdReference = 0;
	isFirewireOpen = false;


	graph = ui.mGraphPlot->graph();
	graph->createPlot("IQData",512);
	graph->setXRange(0, 512);
    graph->setYRange( -130, 4 );
	graph->setYAxis("Power");
	graph->setXAxis("Samples");
	graph->addMarkers(1);
	graph->setDrawGrid(true);
}

DRSControl::~DRSControl()
{

}
void DRSControl::on_actionAbout_triggered(bool checked)
{
	qDebug() << "someText";
	//QWindow *window;

}

void DRSControl::on_actionSave_triggered(bool checked)
{
	qDebug() << "call file saveas";
	DRSControl::fileSaveAs();
}
bool DRSControl::fileSave()
{
	if (fileName.isEmpty())
         return fileSaveAs();

     QTextDocumentWriter writer(fileName);
     bool success = writer.write(ui.recievedTextEdit->document());
     if (success)
         ui.recievedTextEdit->document()->setModified(false);
     return success;
}
bool DRSControl::fileSaveAs()
 {
     QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
		 QString(), tr("TXT files (*.txt);;All Files (*)"));
     if (fn.isEmpty())
         return false;
     if (! (fn.endsWith(".txt", Qt::CaseInsensitive)))
	 {
         fn += ".txt"; // default
	 }
     setCurrentFileName(fn);
     return fileSave();
 }
void DRSControl::setCurrentFileName(const QString &fileName)
{
	this->fileName = fileName;
	ui.recievedTextEdit->document()->setModified(false);

	QString shownName;
	if (fileName.isEmpty())
	 shownName = "untitled.txt";
	else
	 shownName = QFileInfo(fileName).fileName();

	setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
	setWindowModified(false);
}
//-------------------------firewire functions--------------------------------//
void DRSControl::errorDisplay(int warningLevel, QString title, QString text)
{
	switch(warningLevel)
	{
	case 0:
		QMessageBox::information(this, title, text, QMessageBox::Ok);
		break;
	case 1:
		QMessageBox::warning(this, title, text, QMessageBox::Ok);
		break;
	case 2:
		QMessageBox::critical(this, title, text, QMessageBox::Ok);
		break;
	case 3:
		QMessageBox::question(this, title, text, QMessageBox::Ok);
		break;
	default:
		QMessageBox::information(this, title, text, QMessageBox::Ok);
		break;
	}
}
